package com.controller;

import com.controller.vo.ResultVO;
import com.dto.CommentDetailDto;
import com.entity.ProductReviews;
import com.entity.ReviewReplies;
import com.service.ProductReviewsService;
import com.utils.UploadImage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * @ClassName ProductReviewsController
 * @Description TODO
 * @Author Kx
 * @Date 2025/6/9 9:14
 * @Version 1.0
 */
@RestController
@RequestMapping("/productReviews")
public class ProductReviewsController extends BaseController {
    private final ProductReviewsService productReviewsService;

    public ProductReviewsController(ProductReviewsService productReviewsService) {
        this.productReviewsService = productReviewsService;
    }

    /**
     * @param productId   : 商品id
     * @param userId      : 用户id
     * @param content     : 评论内容
     * @param rating      : 评分
     * @param isAnonymous : 是否匿名
     * @param images      : 图片列表
     * @param request     : 请求对象
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @author: kx
     * @date: 2025/6/9 9:16
     * @description: 添加商品评论信息
     */
    @PostMapping("/addProductReview")
    public ResultVO<String> addProductReview(@RequestParam("productId") Integer productId,
                                             @RequestParam("userId") Integer userId,
                                             @RequestParam("content") String content,
                                             @RequestParam("rating") Integer rating,
                                             @RequestParam("isAnonymous") Integer isAnonymous,
                                             @RequestParam(value = "image", required = false) MultipartFile[] images,
                                             HttpServletRequest request) {

        ProductReviews productReviews = new ProductReviews();
        productReviews.setProductId(productId);
        productReviews.setUserId(userId);
        productReviews.setContent(content);
        productReviews.setRating(rating);
        productReviews.setIsAnonymous(isAnonymous);

        // 判断是否需要上传图片
        if (images != null) {
            // 遍历图片列表
            for (MultipartFile image : images) {
                // 上传图片并获取图片路径
                String imagePath = UploadImage.uploadImage(image, request);
                // 设置上传图片路径
                productReviews.getPictures().add(imagePath);
            }
        }
        productReviewsService.addProductReview(productReviews);
        return success("添加商品评论成功");
    }

    /**
     * @param productReviews : 商品评论信息
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @author: kx
     * @date: 2025/6/10 8:44
     * @description: 修改商品评论状态
     */
    @PostMapping("/updateStatus")
    public ResultVO<String> updateStatus(@RequestBody ProductReviews productReviews) {
        productReviewsService.updateStatus(productReviews);
        return success("修改成功");
    }

    /**
     * @param productId : 商品id
     * @return com.controller.vo.ResultVO<java.util.List < com.dto.CommentDetailDto>>
     * @author: kx
     * @date: 2025/6/10 19:17
     * @description: 根据商品id查询商品评论及回复信息
     */
    @GetMapping("/findByProductId")
    public ResultVO<List<CommentDetailDto>> findByProductId(@RequestParam("productId") Integer productId) {
        // 获取该商品下的所有评论及回复信息
        List<CommentDetailDto> commentDetailDtoList = productReviewsService.findByProductId(productId);
        return success(commentDetailDtoList);
    }

    /**
     * @author: kx
     * @date: 2025/6/10 20:44
     * @param reviewReplies : 评论回复信息
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @description: 添加评论回复信息
    */
    @PostMapping("/addReviewReplies")
    public ResultVO<String> addReviewReplies(@RequestBody ReviewReplies reviewReplies){
        productReviewsService.addReviewReplies(reviewReplies);
        return success("添加成功");
    }

    /**
     * @author: kx
     * @date: 2025/6/10 20:57
     * @param reviewReplies : 评论回复信息
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @description: 修改评论回复信息状态
    */
    @PostMapping("/updateReviewReplies")
    public ResultVO<String> updateReviewReplies(@RequestBody ReviewReplies reviewReplies){
        productReviewsService.updateReviewReplies(reviewReplies);
        return success("修改成功");
    }
}
